// /*
//  * This file is part of the microblog <http://code.google.com/p/froggymicroblog/>
//  *
//  * Copyright (c) 2009, bouleetbil  <bouleetbil@frogdev.info>
//  * All rights reserved.
//  * 
//  * Redistribution and use in source and binary forms, with or without modification, are 
//  * permitted provided that the following conditions are met:
//  *
//  * - Redistributions of source code must retain the above copyright notice, this list 
//  *   of conditions and the following disclaimer.
//  * - Redistributions in binary form must reproduce the above copyright notice, this list 
//  *   of conditions and the following disclaimer in the documentation and/or other 
//  *   materials provided with the distribution.
//  * - Neither the name of the Twitterizer nor the names of its contributors may be 
//  *   used to endorse or promote products derived from this software without specific 
//  *   prior written permission.
//  *
//  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
//  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
//  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
//  * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
//  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
//  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
//  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
//  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
//  * POSSIBILITY OF SUCH DAMAGE.
//  */


using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Metier
{
	
	
	public static class Outils
	{
		
		/// <summary>
		/// Détecte les urls dans une chaine
		/// </summary>
		/// <param name="text">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public static string UrlDetection(string text,string origine)
		{
			// match protocol://url
			Regex httpRegex = new Regex(@"([a-z]+://[^)(,!\s]+)", RegexOptions.IgnoreCase);
			// match www. url
			Regex wwwRegex = new Regex(@"(?:^|\s)(www\.[^)(,!\s]+)", RegexOptions.IgnoreCase);
			// match @nickname
			Regex atRegex = new Regex(@"@([^\s:,!]+)", RegexOptions.IgnoreCase);
			
			text = httpRegex.Replace(text, "<a href=\"$1\">$1</a>");
			text = atRegex.Replace(text, "@<a href=\"http://"+origine+"/$1\">$1</a>");
			text = wwwRegex.Replace(text, "<a href=\"http://$1\">$1</a>");
			
			return text;
		}
		/// <summary>
		/// ouvre le navigateur
		/// </summary>
		/// <param name="address">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		public static bool OpenLink(string address) {
    	try {
	        int plat = (int) Environment.OSVersion.Platform;
	        if ((plat != 4) && (plat != 128)) {
	            // Use Microsoft's way of opening sites
	            Process.Start(address);
				return true;
       		 } 
			else 
			{
            // We're on Unix, try gnome-open (used by GNOME), then open
            // (used my MacOS), then Firefox or Konqueror browsers (our last
            // hope).
            string cmdline = String.Format("gnome-open {0} || open {0} || "+
                "midori {0} || firefox {0} || mozilla-firefox {0} || konqueror {0}", address);
            Process proc = Process.Start (cmdline);
 
            // Sleep some time to wait for the shell to return in case of error
            System.Threading.Thread.Sleep(250);
 
            // If the exit code is zero or the process is still running then
            // appearently we have been successful.
            return (!proc.HasExited || proc.ExitCode == 0);
       	 	}
	    } 
		catch  {
	        // We don't want any surprises
	        return false;
			}
		}
		/*public static string GetUrl (Gtk.TextIter start, Gtk.TextIter end)
		{
			
			string url = start.GetSlice (end);

			// FIXME: Needed because the file match is greedy and
			// eats a leading space.
			url = url.Trim ();

			// Simple url massaging.  Add to 'http://' to the front
			// of www.foo.com, 'mailto:' to alex@foo.com, 'file://'
			// to /home/alex/foo.
			if (url.StartsWith ("www."))
				url = "http://" + url;
			else if (url.StartsWith ("/") &&
			                url.LastIndexOf ("/") > 1)
				url = "file://" + url;
			else if (url.StartsWith ("~/"))
				url = "file://" +
				      Path.Combine (Environment.GetEnvironmentVariable ("HOME"),
				                    url.Substring (2));
			else if (url.IndexOf ("@") > 1 &&
			                url.IndexOf (".") > 3 &&
			                !url.StartsWith ("mailto:"))
				url = "mailto:" + url;

			return url;
		}*/
	}
	
}
